package org.pearl.oauth2.resourceserver001demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;

/**
 * Created by TD on 2021/11/17
 */
@Configuration
public class ResourceServerConfigBean {
    // 密码解析器
    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public TokenStore redisTokenStore(RedisConnectionFactory connectionFactory) {
        RedisTokenStore redisTokenStore = new RedisTokenStore(connectionFactory);
        redisTokenStore.setPrefix("user:session:");// 设置前缀
        return redisTokenStore;
    }

    @Bean
    @Primary
    public PearlResourceServerTokenServices defaultTokenServices(RedisConnectionFactory connectionFactory) {
        PearlResourceServerTokenServices resourceServerTokenServices = new PearlResourceServerTokenServices();
        resourceServerTokenServices.setTokenStore(redisTokenStore(connectionFactory));
        return resourceServerTokenServices;
    }
}
